package com.lw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 游戏窗口
 */
public class GameWin extends JFrame {

    GameSelect gameSelect = new GameSelect();
    boolean begin = false;//是否在选择难度 true在选择

    MapBottom mapBottom = new MapBottom();
    MapTop mapTop = new MapTop();

    int width = 2 * GameUtil.OFFSET + GameUtil.MAP_W * GameUtil.SQUARE_LENGTH;
    int height = 4 * GameUtil.OFFSET + GameUtil.MAP_H * GameUtil.SQUARE_LENGTH;

    Image offScreenImage;//解决屏幕闪烁


    @Override
    public void paint(Graphics g) {
        if (GameUtil.state == 3) {
            g.setColor(Color.white);
            g.fillRect(0, 0, 500, 500);
            gameSelect.paintSelf(g);
        } else {
            if (offScreenImage == null) offScreenImage = this.createImage(width, height);
            Graphics gImage = offScreenImage.getGraphics();

            //设置背景色
            gImage.setColor(Color.orange);
            gImage.fillRect(0, 0, width, height);

            mapBottom.paintSelf(gImage);
            mapTop.paintSelf(gImage);

            g.drawImage(offScreenImage, 0, 0, null);
        }


    }

    void launch() {
        GameUtil.START_TIME = System.currentTimeMillis();

        this.setVisible(true);//窗口可见

        if (GameUtil.state == 3) {//难度选择
            this.setSize(500, 500);
        } else {
            this.setSize(width, height);
        }

        this.setLocationRelativeTo(null);//居中显示
        this.setTitle("扫雷");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //鼠标事件监听
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {//鼠标点击
                switch (GameUtil.state) {
                    case 0://游戏中
                        if (e.getButton() == 1) {//鼠标左键
                            GameUtil.MOUSE_X = e.getX();
                            GameUtil.MOUSE_Y = e.getY();
                            GameUtil.LEFT = true;
                        } else if (e.getButton() == 3) {//鼠标右键
                            GameUtil.MOUSE_X = e.getX();
                            GameUtil.MOUSE_Y = e.getY();
                            GameUtil.RIGHT = true;
                        }
                    case 1:
                    case 2:
                        if (e.getButton() == 1) {
                            /*
                            判断鼠标是否点击在游戏状态图标处，即笑脸、哭脸、奖杯图标
                            重置游戏

                            e.getX()<GameUtil.OFFSET+GameUtil.MAP_W*GameUtil.SQUARE_LENGTH/2+GameUtil.SQUARE_LENGTH
                            这里加 SQUARE_LENGTH 是因为游戏状态图标的宽度基本和50像素相似，我准备的是62像素的图片，
                            所以如果追求准确，加SQUARE_LENGTH处可以改成62
                             */
                            if (e.getX() > GameUtil.OFFSET + GameUtil.MAP_W * GameUtil.SQUARE_LENGTH / 2 &&
                                    e.getX() < GameUtil.OFFSET + GameUtil.MAP_W * GameUtil.SQUARE_LENGTH / 2 + GameUtil.SQUARE_LENGTH &&
                                    e.getY() > GameUtil.OFFSET &&
                                    e.getY() < GameUtil.OFFSET + GameUtil.SQUARE_LENGTH) {
                                mapBottom.resetGame();
                                mapTop.resetGame();
                                GameUtil.state = 0;
                                GameUtil.FLAG_NUM = 0;
                                GameUtil.START_TIME = System.currentTimeMillis();
                            }

                        } else if (e.getButton() == 2) {//鼠标滚轮按下
                            GameUtil.state = 3;
                            begin = true;
                        }
                        break;
                    case 3:
                        if (e.getButton() == 1) {//鼠标左键
                            GameUtil.MOUSE_X = e.getX();
                            GameUtil.MOUSE_Y = e.getY();
//                            GameUtil.LEFT = true;
                            begin = gameSelect.clickButton();
                        }
                        break;
                }
            }
        });


        while (true) {
            repaint();
            begin();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
            }
        }

    }

    /**
     * 是否开始游戏
     */
    void begin() {
        if (begin) {
            begin = false;
            gameSelect.clickButton(GameUtil.level);
            dispose();//销毁当前窗口

            GameWin gameWin = new GameWin();
            GameUtil.START_TIME = System.currentTimeMillis();
            GameUtil.FLAG_NUM = 0;
            mapTop.resetGame();
            mapBottom.resetGame();
            gameWin.launch();

        }
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
